﻿using IPC.ListConnect.Profinet.TagThread;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using IPC.LogCommon.DelegateAndEvent;

namespace IPC.ListConnect.Profinet
{
    public class Tag: BaseClass
    {
        private string _name;
        private string _address;
        private DataType _dataType;
        private int _frequency = 0;
        private double _zoom;
        private string _remark;
        private string _value = "未知";
        private ReadWrite _readWrite;
        private string _fullName;
        private int _updateCount = 0;
        private string _realValue = "未知";
        private string _result = "未知";
        private bool _isSuccess = false;
        private System.Timers.Timer _timer;

        /// <summary>
        /// 标签名
        /// </summary>
        public string Name { get => _name; set => _name = value; }
        /// <summary>
        /// 标签地址
        /// </summary>
        public string Address { get => _address; set => _address = value; }
        /// <summary>
        /// 数据类型
        /// </summary>
        public DataType DataType { get => _dataType; set => _dataType = value; }
        /// <summary>
        /// 扫描频率
        /// </summary>
        public int Frequency { get => _frequency; set
            {
                if (_frequency != value)
                {
                    _frequency = value;
                    if (_frequency > 0)
                    {
                        TagThreadPool.RemoveTag(this);
                        TagThreadPool.AddTag(this);
                    }
                }
            }
        }
        /// <summary>
        /// 缩放
        /// </summary>
        public double Zoom { get => _zoom; set => _zoom = value; }
        /// <summary>
        /// 说明
        /// </summary>
        public string Remark { get => _remark; set => _remark = value; }

        /// <summary>
        /// 实际值
        /// </summary>
        public string RealValue
        {
            get
            {
                return _realValue;
            }
            set
            {
                _realValue = value;
            }
        }

        public bool IsSuccess { get => _isSuccess;set => _isSuccess = value; }
        /// <summary>
        /// 失败原因
        /// </summary>
        public string Result
        {
            get
            {
                return _result;
            }
            set
            {
                _result = value;
            }
        }

        /// <summary>
        /// 值
        /// </summary>
        public string Value 
        { 
            get => _value; 
            set
            {
                if (_value != value)
                {
                    _value = value;
                    if(UpdateCount == int.MaxValue)
                    {
                        UpdateCount = 0;
                    }
                    UpdateCount++;
                    DelegateAndEvent.Instance.RefreshFrmWhenValueUpdate(_value, _fullName,_updateCount);
                }
            }
        }
        /// <summary>
        /// 客户端访问类型
        /// </summary>
        public ReadWrite ReadWrite { get => _readWrite; set => _readWrite = value; }
        /// <summary>
        /// 全名，包括通道，设备，组
        /// </summary>
        public string FullName { get => _fullName; set => _fullName = value; }
        public int UpdateCount { get => _updateCount; set => _updateCount = value; }

        public Tag()
        {

        }

        public Tag(string guid,string name,string address,string remark,DataType dataType,ReadWrite readWrite,int frequency,double zoom,string fullName):base()
        {
            Zoom = zoom;
            FullName = fullName;
            Guid = guid;
            Name = name;
            Address = address;
            Remark = remark;
            DataType = dataType;
            ReadWrite = readWrite;
            Frequency = frequency;
            DataInit();
            Read();
        }

        public void WillClose()
        {
            TagThreadPool.RemoveTag(this);
        }

        public void Read()
        {
            TagThreadPool.AddTag(this);
        }

        private void DataInit()
        {
            if (this.DataType == DataType.UInt16 || this.DataType == DataType.UInt32 || this.DataType == DataType.UInt64 || this.DataType == DataType.Int16 || this.DataType == DataType.Int32 || this.DataType == DataType.Int64 ||
                    this.DataType == DataType.Float || this.DataType == DataType.Double)
            {
                this.Value = (this.RealValue.ToDouble(0) * this.Zoom).ToString();
                
            }
            else
            {
                this.Value = this.RealValue;
            }
        }

    }
}
